package com.fire.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.coupon.req.DelCouponReq;
import com.fire.bo.online.req.OnlineReq;
import com.fire.bo.online.res.OnlineRes;
import com.fire.common.RspObjBean;
import com.fire.service.OnlineService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/online")
@Api(value = "线下支付", description = "线下支付")
public class OnlineController
{
	@Autowired
	private OnlineService onlineService;
	
	/**
	 * 修改状态
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "修改状态", notes = "收藏数据统计")
	@RequestMapping(value = "/updateOnline", method = RequestMethod.POST)
	public @ResponseBody RspObjBean updateOnline(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			onlineService.updateOnline(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 删除
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "删除", notes = "删除")
	@RequestMapping(value = "/delOnline", method = RequestMethod.POST)
	public @ResponseBody RspObjBean update(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			onlineService.delOnline(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "在线支付列表", notes = "在线支付列表", response = OnlineRes.class)
    @RequestMapping(value = "/onlineList", method = RequestMethod.POST)
    public @ResponseBody RspObjBean onlineList(@RequestBody @Valid OnlineReq bean, BindingResult bindingResult)
    {
        // 非空验证
        if (bindingResult.hasErrors())
        {
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE, bindingResult.getFieldError().getDefaultMessage());
        }

        try
        {
        	List<OnlineRes> list = onlineService.onlineList(bean);
            // 根据上级类别key得到类别集合
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE, ErrRsp.SUCCESS_CODE_DESC, list,onlineService.onlineListCount(bean));
        }
        catch (Exception e)
        {
        	e.printStackTrace();
            return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE, ErrRsp.SYSTEM_ERROR_DESC);
        }
    }
	
}
